package com.xiaodong.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaodong.reggie.currency.BaseContest;
import com.xiaodong.reggie.currency.CostomException;
import com.xiaodong.reggie.currency.R;
import com.xiaodong.reggie.dto.DishDto;
import com.xiaodong.reggie.dto.OrdersDto;
import com.xiaodong.reggie.entity.*;
import com.xiaodong.reggie.service.AddressBookService;
import com.xiaodong.reggie.service.OrderDetallService;
import com.xiaodong.reggie.service.OrdersService;
import com.xiaodong.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.annotation.OrderUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/order")
public class OrdersController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    @Autowired
    private AddressBookService addressBookService;

    @Autowired
    private OrdersService ordersService;

    @Autowired
    private OrderDetallService orderDetallService;

    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){

        ordersService.goods(orders);
        return R.success("下单成功");

    }

    @GetMapping("/userPage")
    public R<Page<OrdersDto>> page(int page, int pageSize){

        Page<Orders> pageInfo = new Page(page, pageSize);
        Page<OrdersDto> pageInfo1 = new Page();

        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper();

        queryWrapper.orderByDesc(Orders::getOrderTime);

        ordersService.page(pageInfo, queryWrapper);

        //拷贝对象
        BeanUtils.copyProperties(pageInfo, pageInfo1, "records");

        List<Orders> records = pageInfo.getRecords();

        List<OrdersDto> list = records.stream().map((item) -> {

            OrdersDto ordersDto = new OrdersDto();
            //拷贝对象
            BeanUtils.copyProperties(item, ordersDto);
            Long orderId = item.getId();
            LambdaQueryWrapper<OrderDetail> queryWrapper1=new LambdaQueryWrapper();
            queryWrapper1.eq(OrderDetail::getOrderId,orderId);
            List<OrderDetail> duo = orderDetallService.list(queryWrapper1);
            ordersDto.setOrderDetails(duo);

            AddressBook byId = addressBookService.getById(item.getAddressBookId());

            ordersDto.setPhone(byId.getPhone());
            ordersDto.setAddress(byId.getDetail());
            ordersDto.setConsignee(byId.getConsignee());

            return ordersDto;

        }).collect(Collectors.toList());

        pageInfo1.setRecords(list);

        return R.success(pageInfo1);

    }

}
